<template>
	<view class="full-page p-30rpx">
		<u-form ref="uForm" class="form-box" :model="formData">
			<u-cell-group>
				<u-form-item label-width="160rpx" name="oldpwd" prop="oldpwd" label="原始密码" :arrow="false">
					<u-input v-model="formData.oldpwd" type="password" placeholder="请输入原始密码" />
				</u-form-item>
				<u-form-item label-width="160rpx" name="newpwd" prop="newpwd" label="新密码" :arrow="false">
					<u-input v-model="formData.newpwd" type="password" placeholder="请输入新密码" />
				</u-form-item>
				<u-form-item label-width="160rpx" name="repwd" prop="repwd" label="确认密码" :arrow="false">
					<u-input v-model="formData.repwd" type="password" placeholder="请输入确认密码" />
				</u-form-item>
			</u-cell-group>
		</u-form>
		<u-button class="mt-16rpx" type="primary" @click="handleSubmit">提交</u-button>
	</view>
</template>

<script setup lang="ts">
	import { ref } from 'vue';
	import { isEmptys, ktlapi, ktluser } from '@/utils';
	
	const formData = ref({
		oldpwd: '',
		newpwd: '',
		repwd: ''
	})

	const handleSubmit = () => {
		if(isEmptys([formData.value.oldpwd, formData.value.newpwd, formData.value.repwd])) {
			uni.showToast({
				title: '请将密码信息填写完整！',
				icon: 'none'
			})
			return
		}
		
		if(formData.value.newpwd !== formData.value.repwd){
			uni.showToast({
				title: '新密码与确认密码不一致！',
				icon: 'none'
			})
			return
		}
		
		ktlapi({
			action: 'Cus_UpdataPwd',
			data: {
				ID: ktluser.BaseInfo().AccountID,
				...formData.value
			}
		}).then((res: any) => {
			if(res.data[0].status === 1){
				uni.showToast({
					title: res.data[0].msg,
					icon: 'none'
				})
				setTimeout(() => {
					ktluser.logoff();
				}, 200)
			} else {
				uni.showToast({
					title: res.data[0].msg,
					icon: 'none'
				})
			}
		})
	}
</script>

<style>
	
	.form-box{
		flex: 1;
	}
</style>